package com.hnucm.ad0301;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CircleView extends View {

    public static List<Circle>circleList=new ArrayList<>();
    public float viewWidth;
    public float viewHeight;

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 画圆
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        //获取画布的宽高
        viewWidth=canvas.getWidth();
        viewHeight=canvas.getHeight();
        //设置画布的背景色为黑色
        this.setBackgroundColor(Color.BLACK);
        //有了画布canvas,我们还需要一支画笔
        Paint paint=new Paint();
        Paint textPaint=new Paint();
        //遍历列表画圆
        for(int i=0;i<circleList.size();++i){

            //圆画自己
            Circle circle=circleList.get(i);
            circle.drawSelf(canvas,paint);

            //画圆上的数字
            textPaint.setColor(Color.WHITE);
            textPaint.setTextSize(80);
            //设置基线为center
            textPaint.setTextAlign(Paint.Align.CENTER);
            Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            //为基线到字体上边框的距离
            float top = fontMetrics.top;
            //为基线到字体下边框的距离
            float bottom = fontMetrics.bottom;
            int baseLineY=(int)(circle.y-top/2-bottom/2);
            canvas.drawText((i+1)+"",circle.x,baseLineY, textPaint);

        }
    }

    /*
    * 添加一个圆
    * */
    public void addCircle(){
        //在view的宽高范围内，随机生成一个坐标
        float x=(float) (Math.random()*viewWidth);
        float y=(float) (Math.random()*viewHeight);
        //约束条件，不让圆超出画布范围
        //宽度约束
        if(x<120f){
            x=120f;
        }else if(x>viewWidth-120f){
            x=viewWidth-120f;
        }
        //高度约束
        if(y<120f){
            y=120f;
        }else if(y>viewHeight-120f){
            y=viewHeight-120f;
        }
        //将圆添加到列表中
        CircleView.Circle circle=new CircleView.Circle(x,y,CircleView.getRandomColor(),0);
        CircleView.circleList.add(circle);
    }

    /**
     * 移除一个圆
     */
    public void removeCircle(){
        if(!circleList.isEmpty()){
            circleList.remove(circleList.size()-1);
        }
    }

    /**
     * 清空圆
     */
    public void clearCircle(){
        circleList.clear();
    }
    /**
     * 圆形类
     */
    class Circle{
        /** 初始圆心的坐标 */
        private  float originX,originY;
        float x,y;
        final float RADIUS=120;
        final int color;
        final int pointId;

        /**
         * 圆的构造方法
         * @param x 圆心横坐标
         * @param y 圆心纵坐标
         * @param color 圆的颜色
         * @param pointId 圆的id
         */
        public Circle(float x, float y, int color, int pointId) {
            this.x = x;
            this.y = y;
            this.color=color;
            this.pointId = pointId;

            this.originX=x;
            this.originY=y;
        }
    /**
     * 在画布上画圆
     * @param canvas 画布
     * @param paint 画笔
     */
        public void drawSelf(Canvas canvas,Paint paint){
            float distance=getDistance(originX,originY,x,y);
            int zoomRadius =(int) (RADIUS+distance/5);
            paint.setColor(color);
            canvas.drawCircle(x,y,zoomRadius,paint);
        }

        private float getDistance(float originX, float originY, float x, float y) {
            return (float) Math.sqrt(Math.pow(originX-x,2)+Math.pow(originY-y,2));
        }
    }

    public static int getRandomColor(){
        Random random=new Random();
        int r=random.nextInt(256);
        int g=random.nextInt(256);
        int b=random.nextInt(256);
        return Color.argb(200,r,g,b);
    }

    /**
     * 根据触点ID,从圆形列表中查找
     * @param pointId 触点ID
     * @return 返回圆对象
     */
    public Circle getCircle(int pointId){
        for(Circle circle:circleList){
            if(circle.pointId==pointId){
                return circle;
            }
        }
        return null;
    }

    /**
     * 监听触摸事件
     * @param event 事件
     * @return 返回触控事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        event.getAction();
        //低八位动作特征
        int actionCode=event.getActionMasked();
        //高八位触点序号
        int actionIndex=event.getActionIndex();
        int pointId=event.getPointerId(actionIndex);
        switch (actionCode){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:{
                Circle circle=new Circle(event.getX(), event.getY(), getRandomColor(),pointId);
                circleList.add(circle);}
                //以当前触点按下的坐标添加一个圆
                break;
            case MotionEvent.ACTION_MOVE: {
                //使圆形位置跟随触点移动
                if(!circleList.isEmpty()){
                    for (int i = 0; i < event.getPointerCount(); ++i) {
                        int id = event.getPointerId(i);
                        getCircle(id).x = event.getX(i);
                        getCircle(id).y = event.getY(i);
                    }
                }
            }break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:{
                //移除当前触点抬起位置的圆
                circleList.remove(getCircle(pointId));
            }break;
            default:
                break;
        }
        //在子线程中重新执行一次
        postInvalidate();
        return true;
    }
}
